/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ppsa.Servlet;

import com.ppsa.bean.CargoBean;
import com.ppsa.bean.EmpleadoBean;
import com.ppsa.bean.UsuarioBean;
import com.ppsa.common.Util;
import com.ppsa.logic.ConsultaLogic;
import com.ppsa.logic.MantenimientoLogic;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.ObjectUtils;
import org.json.JSONObject;

/**
 *
 * @author jaburtop
 */
@WebServlet(name = "ServletBitacora", urlPatterns = {"/ServletBitacora"})
public class ServletBitacora extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest( HttpServletRequest request, HttpServletResponse response )
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        RequestDispatcher dispatcher = null;
        HttpSession objL_sesion = objL_sesion = request.getSession(true);
        UsuarioBean objL_usuarioBeanSession = new UsuarioBean();
        objL_usuarioBeanSession = (UsuarioBean) objL_sesion.getAttribute("objL_usuario");
        MantenimientoLogic objL_mantenimiento = null;
        ConsultaLogic objL_consulta;
        JSONObject objL_json = null;
        ArrayList<JSONObject> lstL_JSON = null;
        try {
            String strpv_metodo = Util.getParameterString(request, "metodo", null, "");    
            System.out.println("metodo -> " +  strpv_metodo);

            if( strpv_metodo.equals("obtenerAreaBitacora") ){
                String strpv_codigoDocumento = ObjectUtils.toString(request.getParameter("strpv_codigoDocumento"));
                String strpv_codigoVersion = ObjectUtils.toString(request.getParameter("strpv_codigoVersion"));
                
                objL_consulta = new ConsultaLogic();
                lstL_JSON = new ArrayList<JSONObject>();
                for( CargoBean obj : objL_consulta.obtenerAreaCargoBitacora(strpv_codigoDocumento, strpv_codigoVersion) ) {
                    objL_json = new JSONObject();
                    objL_json.put("strpv_nombreArea", obj.getStrpv_nombreArea());
                    objL_json.put("strpv_nombreCargo", obj.getStrpv_nombreCargo());
                    lstL_JSON.add(objL_json);
                }
                Util.returnJSONWriter(response, lstL_JSON.toString());
            }
        } catch ( Exception ex ) {
            ex.printStackTrace();
        } finally {
            //out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet( HttpServletRequest request, HttpServletResponse response )
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost( HttpServletRequest request, HttpServletResponse response )
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
